//
//  DiskItemCell.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/6/12.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "DiskItemCell.h"

@implementation DiskItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    self.conView.layer.cornerRadius = globalRadius;
    self.formatBt.layer.cornerRadius = globalRadius;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(Disks *)model {
    [[RACObserve(model, diskId) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        self.titleLb.text = [NSString stringWithFormat:@"硬盘标识：%@", model.diskId ? model.diskId : @"未知"];
    }];
    [[RACObserve(model, storage) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        int used = 0;
        used = isNil(model.storageUsed) ? [model.storageUsed doubleValue] : 0;
        used = used/1024;
        int total = 0;
        total = isNil(model.storage) ? [model.storage doubleValue] : 0;
        total = total/1024;
        self.spaceLb.text = [NSString stringWithFormat:@"总空间：%dGB, 可用空间：%dGB", total, total-used];
    }];
    [[RACObserve(model, status) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        NSString *text = [NSString stringWithFormat:@"状态：%@", isNil(model.statusTips) ? model.statusTips : @"未知"];
        if (model.status && [model.status intValue] == 0) {
            self.stateLb.text = text;
        } else {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
            [attributedString addAttribute:NSForegroundColorAttributeName value:colorWithString(@"#8D8D8D") range:NSMakeRange(0, 3)];
            [attributedString addAttribute:NSForegroundColorAttributeName value:colorWithString(@"#FF0000") range:NSMakeRange(3, text.length-3)];
            [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(3, text.length-3)];
            self.stateLb.attributedText = attributedString;
        }
    }];
    [[[self.formatBt rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        if (self->mBlock) {
            self->mBlock(model);
        }
    }];
}

- (void)setBlock:(void (^)(Disks *item))block {
    mBlock = [block copy];
}

@end
